package com.ykdl.member.kid.adapters;

import java.util.ArrayList;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.MyLetterAdapter.ViewHolder;
import com.ykdl.member.kid.beans.MessageBean;
import com.ykdl.member.kid.beans.SystemMessageBean;
import com.ykdl.member.kid.doctor.DoctorInfoActivity;
import com.ykdl.member.kid.image.ImageOptions;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.person.PersonCenterActivity;
import com.ykdl.member.kid.util.DateUtil;
import com.ykdl.member.kid.widget.AsyncImageView;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MessageAdapter extends BaseAdapter{
	private Activity mContext ;
	private ArrayList<SystemMessageBean>  list;
	private ImageOptions options ;
	private final static int NOTIFICATION_VERB_OFFICIAL = 3 ;//官方消息类型
	private final static int NOTIFICATION_VERB_CAMPAIGN = 4 ;// 活动消息类型
	private final static int NOTIFICATION_VERB_POST = 5 ;// 用户帖子消息类型
	private final static int NOTIFICATION_VERB_POST_REPLY = 6 ;//用户帖子消息类型 二级回复
	private final static int NOTIFICATION_VERB_CLINIC_POST = 7 ;// 问医生帖子消息类型
	private BitmapUtils bitmapUtil ;
	private boolean isread  ;
	public MessageAdapter(Activity context ,Boolean isread){
		this.mContext = context;
		options =ImageOptionsFactory.getSmallPortraitOptions(mContext);
		bitmapUtil = new BitmapUtils(mContext);
		this.isread = isread;
	}
	public void setlist(ArrayList<SystemMessageBean>  list) {
		this.list = list ;
	}
	public void setisread(Boolean isread) {
		this.isread = isread;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (list != null) {
			return list.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if (getCount() != 0) {
			return list.get(position-1);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null ;
		if (convertView==null) {
			convertView = RelativeLayout.inflate(mContext,
					R.layout.messageadapter_item, null);
			holder = new ViewHolder();
			 holder.user_head =  (ImageView) convertView.findViewById(R.id.user_head);
			 holder.user_name=  (TextView) convertView.findViewById(R.id.user_name);
			 holder.user_grade=  (TextView) convertView.findViewById(R.id.grade);
			 holder.personnum=  (TextView) convertView.findViewById(R.id.personnum);
			 holder.user_content=  (TextView) convertView.findViewById(R.id.user_content);
			 holder.doctor_grade =  (ImageView) convertView.findViewById(R.id.doctor_grade);
			 holder.user_time=  (TextView) convertView.findViewById(R.id.user_time);
			 convertView.setTag(holder);
		}else {
			holder = (ViewHolder)convertView.getTag();
		}
		setViewData(holder,list.get(position));
		return convertView;
	}
	public void setViewData(ViewHolder holder,final SystemMessageBean bean) {
		if (isread) {
			if (bean.getUnread_count()>0) {
				holder.personnum.setVisibility(View.VISIBLE);
				holder.personnum.setText(bean.getUnread_count()+"");
			}else {
				holder.personnum.setVisibility(View.INVISIBLE);
			}
		}else {
			holder.personnum.setVisibility(View.INVISIBLE);
		}
		
		String time ;
		switch (bean.getVerb()) {
		case NOTIFICATION_VERB_OFFICIAL:
			//官方消息类型
			holder.user_head.setBackgroundResource(R.drawable.defualt_icon);
			holder.user_grade.setVisibility(View.INVISIBLE);
			holder.doctor_grade.setVisibility(View.GONE);
			holder.user_content.setText(bean.getExtra_context().getOfficial_message().getContent());
			 time = DateUtil.gettopicDate(bean.getExtra_context().getOfficial_message().getCreated_time());
			 holder.user_time.setText(time);
			 holder.user_name.setText(bean.getExtra_context().getOfficial_message().getFrom_actor().getDisplay_name());
			break;
		case NOTIFICATION_VERB_CAMPAIGN:
			holder.user_head.setBackgroundResource(R.drawable.defualt_icon);
			holder.user_grade.setVisibility(View.INVISIBLE);
			holder.doctor_grade.setVisibility(View.GONE);
			holder.doctor_grade.setVisibility(View.GONE);
			holder.user_content.setText(bean.getExtra_context().getOfficial_message().getContent());
			 time = DateUtil.gettopicDate(bean.getExtra_context().getOfficial_message().getCreated_time());
			 holder.user_time.setText(time);
			 holder.user_name.setText(bean.getExtra_context().getOfficial_message().getFrom_actor().getDisplay_name());
			// 活动消息类型
			break;
		case NOTIFICATION_VERB_POST:
			holder.user_grade.setVisibility(View.VISIBLE);
			holder.doctor_grade.setVisibility(View.GONE);
			if (bean.getExtra_context().getPost().getPost_actor().getAvatar_file_meta()!=null) {
				String url = bean.getExtra_context().getPost().getPost_actor().getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
				bitmapUtil.display(holder.user_head, url);
			} 
			holder.user_grade.setText(bean.getExtra_context().getPost().getPost_actor().getLevel()+"");
			 time = DateUtil.gettopicDate(bean.getExtra_context().getPost().getPost_time());
			 holder.user_time.setText(time);
			holder.user_name.setText(bean.getExtra_context().getTopic().getTitle());
			holder.user_content.setText(bean.getExtra_context().getPost().getMessage());
			holder.user_head.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(mContext, PersonCenterActivity.class);
					intent.putExtra(PersonCenterActivity.STR_ACTOR_ID, bean.getExtra_context().getPost().getPost_actor().getActor_id());
					mContext.startActivity(intent);
				}
			});
			// 用户帖子消息类型
			break;
		case NOTIFICATION_VERB_POST_REPLY:
			holder.user_grade.setVisibility(View.VISIBLE);
			holder.doctor_grade.setVisibility(View.GONE);
			if (bean.getExtra_context().getPost().getPost_actor().getAvatar_file_meta()!=null) {
				String url = bean.getExtra_context().getPost().getPost_actor().getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
				bitmapUtil.display(holder.user_head, url);
			}
			holder.user_grade.setText(bean.getExtra_context().getPost().getPost_actor().getLevel()+"");
			 time = DateUtil.gettopicDate(bean.getExtra_context().getPost().getPost_time());
			 holder.user_time.setText(time);
			holder.user_name.setText(bean.getExtra_context().getPost().getPost_actor_display_name());
			holder.user_content.setText(bean.getExtra_context().getPost().getMessage());
			holder.user_head.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(mContext, PersonCenterActivity.class);
					intent.putExtra(PersonCenterActivity.STR_ACTOR_ID, bean.getExtra_context().getPost().getPost_actor().getActor_id());
					mContext.startActivity(intent);
				}
			});
			//用户帖子消息类型 二级回复
			break;
		case NOTIFICATION_VERB_CLINIC_POST:
			holder.user_grade.setVisibility(View.GONE);
			holder.doctor_grade.setVisibility(View.VISIBLE);
			if (bean.getExtra_context().getPost().getPost_actor().getAvatar_file_meta()!=null) {
				String url = bean.getExtra_context().getPost().getPost_actor().getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
				bitmapUtil.display(holder.user_head, url);
			} 
			holder.user_grade.setText(bean.getExtra_context().getPost().getPost_actor().getLevel()+"");
			holder.user_name.setText(bean.getExtra_context().getPost().getPost_actor().getDisplay_name());
//			holder.user_name.setText(bean.getExtra_context().getTopic().getMessage());
			holder.user_content.setText(bean.getExtra_context().getPost().getMessage());
			// 问医生帖子消息类型
			holder.user_head.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(mContext, DoctorInfoActivity.class);
					intent.putExtra("doctor_id", bean.getExtra_context().getPost().getPost_actor().getActor_id()+"");
					mContext.startActivity(intent);
				}
			});
			break;
		default:
			break;
		}
	}
	class ViewHolder {
		ImageView user_head;
		 TextView  user_name,personnum,user_grade,
		user_content,user_time;
		 ImageView doctor_grade ;
	}

}
